package logging;

import Filter.FilterStrategy;

public class FilterCustom1 implements FilterStrategy{
    
	@Override
	public boolean apply(Record record) {
		String s[] = this.splitMessage(record);
		int numberLine = foundLineNumber(s);
		return isOddNumber(numberLine);
	}

	private int foundLineNumber(String string[]){
		for (String s : string){
			try{
				int lineNumber = Integer.parseInt(s.trim());
				return lineNumber;
			}catch (NumberFormatException e){
				continue;
			}	
		}
		return -1;
	}
	
	private boolean isOddNumber(int numberLine) {
		if (numberLine == -1)
			return false;
		return numberLine%2 != 0;
					
	}
	
	private String[] splitMessage(Record record){
		if (record.getTypeFormat().equals(LoggerConstants.typeFormatDefault))
			return splitMessageTrad(record);
		return splitMessageJson(record);
				
	}
	
	private String[] splitMessageTrad(Record record){
		String message = record.getMessageFormatted();
		return message.split(record.getSeparator());
	}
	
	private String[] splitMessageJson(Record record){
		String message = record.getMessageFormatted();
		//elimino {}
		message = message.substring(1, message.length()-1);
		//separo por espacio y por coma
		return message.split("[\\s\\,]");
	}

}
